package com.ckzp.jfinal.system.validate;

import com.ckzp.core.json.Jmode;
import com.ckzp.core.json.JsonResult;
import com.ckzp.jfinal.system.cache.SysAppCache;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * Created by 51594 on 2023/2/24.
 */
public class AccessTokenValidate implements Interceptor {

    /**
     * 拦截器，判断 accesstoken是否合法是否有效
     * @param invocation
     */
    public void intercept(Invocation invocation) {
        Controller controller = invocation.getController();
        String access_token = controller.get("access_token", "");
        Jmode jmode = SysAppCache.checkAccessToken(access_token);
        int aid = jmode.getInt("aid");
        if (jmode.getInt("error") > 0 || aid == 0) {
            controller.renderJson(jmode);
        } else {
            controller.setAttr("aid", aid);
            invocation.invoke();
        }
    }
}
